package jsu.ask.controller;

import jsu.ask.pojo.User;
import jsu.ask.service.UserService;
import jsu.ask.util.DataSourceContextHolder;
import jsu.ask.util.DataSourceInterceptor;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    //controller 调 service层
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;
    private int id;

    //查询全部的用户，并且返回到一个用户展示页面
    @RequestMapping("/allUser")
    public String userList(Model model){
        List<User> users = userService.queryAllUser();
        model.addAttribute("list",users);
        return "allUser";
    }
    //跳转到登录界面
    @RequestMapping("/login")
    public String login(){
        return "login";
    }
    //跳转到注册界面
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    //跳转到主界面
    @RequestMapping("/index-2")
    public String index(){
        return "index-2";
    }
    //跳转到问答界面
    @RequestMapping("/quest")
    public String quest(){
        return "redirect:/Chat.html";
    }
    //登录验证，查询用户
    @RequestMapping("/loginVerift")
    public String queryUser(String username,String userpassword,Model model){
        if(username.equals("root") && userpassword.equals("root")){
            return "redirect:/manager/showPage";
        }else {
            User user=userService.queryUser(username, userpassword);
            if(user==null){
                model.addAttribute("error","用户名或密码错误");
                return "login";
            }
            model.addAttribute("user",user);
            return "index-2";
        }
    }

    @RequestMapping("/toIndex/{username}")
    public String toIndex(Model model,@PathVariable("username")String username){
            User user=userService.queryUserByName(username);
            System.out.println(user.toString());
            model.addAttribute("user",user);
            return "index-2";
    }
    //注册
    @RequestMapping("/registerVerift")
    public String addUser(User user,Model model){
        if(userService.queryUserByName(user.getUsername())!=null){
            model.addAttribute("error","该用户已存在，请使用其他的用户名");
            return "register";
        }
        else {
            user.setBeginTime(new Date());
            userService.addUser(user);
            return "redirect:/user/login";
        }
    }
    @RequestMapping(value = "/sendVerift",method = RequestMethod.POST)
    public void sendVerift(String email) throws EmailException {
//        SimpleEmail sendEmail = new SimpleEmail();
//        sendEmail.setHostName("smtp.qq.com");// 邮件服务器域名
//        sendEmail.setSmtpPort(465);// 邮件服务器smtp协议端口
//        sendEmail.setAuthentication("2335447694@qq.com","TY13859593460");//登陆邮件服务器的用户名和密码
//        sendEmail.addTo("2243384266@qq.com");//接收人
//        sendEmail.setFrom ( "2335447694@qq.com", "2335447694@qq.com" );// 发送人
//        sendEmail.setSSLOnConnect(true);
//        sendEmail.setCharset("UTF-8");//编码格式
//        sendEmail.setSubject ( "I LOVE YOU");// 标题
//        sendEmail.setMsg ( "Happy 15 week old love day" );//邮件内容
//        System.out.println ( sendEmail.getSmtpPort() );
//        sendEmail.send();//发送邮件
//        System.out.println ( "Send email successful!" );
//        System.out.println ( sendEmail.getSmtpPort() );
        System.out.println(email);
    }
    //传递用户名
//    @RequestMapping("/space")
//    public String userName(String username){
//        return username;
//    }

}
